#define _GNU_SOURCE
#include "fastxml.h"

void print_node_info(xml_node_t *node, int depth) {
    if (!node) return;
    
    for (int i = 0; i < depth; i++) printf("  ");
    
    if (node->type == XML_NODE_ELEMENT) {
        printf("Element: %s", node->name ? node->name : "unknown");
        
        // 打印属性
        xml_attribute_t *attr = node->attributes;
        if (attr) {
            printf(" [");
            while (attr) {
                printf("%s=\"%s\"", 
                       attr->name ? attr->name : "unknown", 
                       attr->value ? attr->value : "unknown");
                attr = attr->next;
                if (attr) printf(", ");
            }
            printf("]");
        }
        
        if (node->content) {
            printf(" = \"%s\"", node->content);
        }
        printf("\n");
        
        // 递归打印子节点
        xml_node_t *child = node->children;
        while (child) {
            print_node_info(child, depth + 1);
            child = child->next;
        }
    } else if (node->type == XML_NODE_COMMENT) {
        printf("Comment: %s\n", node->content ? node->content : "");
    }
}

int main(int argc, char *argv[]) {
    printf("FastXML Parser Demo\n");
    printf("==================\n\n");
    
    // 测试XML字符串
    const char *test_xml = 
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
        "<bookstore>\n"
        "  <!-- 这是一个书店的XML示例 -->\n"
        "  <book id=\"1\" category=\"fiction\">\n"
        "    <title>The Great Gatsby</title>\n"
        "    <author>F. Scott Fitzgerald</author>\n"
        "    <price currency=\"USD\">10.99</price>\n"
        "    <available>true</available>\n"
        "  </book>\n"
        "  <book id=\"2\" category=\"science\">\n"
        "    <title>A Brief History of Time</title>\n"
        "    <author>Stephen Hawking</author>\n"
        "    <price currency=\"USD\">15.99</price>\n"
        "    <available>false</available>\n"
        "  </book>\n"
        "  <metadata>\n"
        "    <created>2024-01-01</created>\n"
        "    <updated>2024-01-15</updated>\n"
        "  </metadata>\n"
        "</bookstore>\n";
    
    printf("1. 解析XML字符串:\n");
    printf("--------------------------------\n");
    
    xml_document_t *doc = xml_parse_string(test_xml);
    if (doc) {
        printf("XML解析成功!\n\n");
        
        printf("2. 文档结构:\n");
        printf("--------------------------------\n");
        print_node_info(doc->root, 0);
        
        printf("\n3. 查找特定节点:\n");
        printf("--------------------------------\n");
        
        // 查找第一本书
        xml_node_t *first_book = xml_find_node(doc->root, "book");
        if (first_book) {
            printf("找到第一本书:\n");
            const char *id = xml_get_attribute(first_book, "id");
            const char *category = xml_get_attribute(first_book, "category");
            printf("  ID: %s, 类别: %s\n", id ? id : "unknown", category ? category : "unknown");
            
            xml_node_t *title = xml_find_node(first_book, "title");
            xml_node_t *author = xml_find_node(first_book, "author");
            xml_node_t *price = xml_find_node(first_book, "price");
            
            printf("  标题: %s\n", title ? xml_get_node_content(title) : "unknown");
            printf("  作者: %s\n", author ? xml_get_node_content(author) : "unknown");
            printf("  价格: %s %s\n", 
                   price ? xml_get_node_content(price) : "unknown",
                   price ? xml_get_attribute(price, "currency") : "");
        }
        
        printf("\n4. 根据属性查找节点:\n");
        printf("--------------------------------\n");
        
        xml_node_t *science_book = xml_find_node_by_attribute(doc->root, "category", "science");
        if (science_book) {
            printf("找到科学类书籍:\n");
            xml_node_t *title = xml_find_node(science_book, "title");
            printf("  标题: %s\n", title ? xml_get_node_content(title) : "unknown");
        }
        
        printf("\n5. 输出完整XML:\n");
        printf("--------------------------------\n");
        xml_print_document(doc, stdout);
        
        xml_free_document(doc);
    } else {
        printf("XML解析失败!\n");
    }
    
    // 如果提供了文件参数，尝试解析文件
    if (argc > 1) {
        printf("\n\n6. 解析文件: %s\n", argv[1]);
        printf("================================\n");
        
        xml_document_t *file_doc = xml_parse_file(argv[1]);
        if (file_doc) {
            printf("文件解析成功!\n");
            print_node_info(file_doc->root, 0);
            xml_free_document(file_doc);
        } else {
            printf("文件解析失败!\n");
        }
    }
    
    return 0;
}